﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using Geotagger;
using System.Drawing;
using System.Drawing.Imaging;

namespace GeoTaggerCLI
{
  class Program
  {
    static void Main(string[] args)
    {



      foreach (string s in args)
      {
        s.ToUpper();
        if (s == "-VERBOSE")
        {
          TextWriterTraceListener writer = new TextWriterTraceListener(System.Console.Out);
          Trace.Listeners.Add(writer);
          break;
        }
      }

      if (args.Length < 2)
      {
        PrintUsage();

      }
      switch(args[0].ToUpper()){
      
        case "-?":
          PrintUsage();
          break;

        case "-FOLDER":
          string folder = args[1];
          string tcxpath = FindTCXFile(folder);
          Geotagger.Geotagger tagger = new Geotagger.Geotagger();
          tagger.ImageFolder = folder;
          tagger.TcxPath = tcxpath;
          tagger.SetGeocodeData();
          break;

        case "-FILE":
          string fileName = args[2];
          string latitude = args[3];
          string longitude = args[4];
          string altitude = args[5];
          Geotagger.Geotagger.SetGeocodeDataManually(fileName, new GeoDataPoint(latitude, longitude, altitude));
          break;

        default:
          PrintUsage();
          break;
      }

    }

    private static string FindTCXFile(string folder) { 
    
      string returnValue = "";
      if(Directory.Exists(folder)){
      
        string[] f = Directory.GetFiles(folder,"*.tcx");
        returnValue = f[0];
      }
      return returnValue;
      
    }

    private static void PrintUsage()
    {

      Console.Out.WriteLine("USAGE: GeoTaggerCLI arguments");
      Console.Out.WriteLine("       Where arguments is any combination of the following:");
      Console.Out.WriteLine("       -VERBOSE Show verbose status messages");
      Console.Out.WriteLine("       -FOLDER [foldername]");
      Console.Out.WriteLine("               processes a given folder.  Expects to find .tcx file in the folder");
      Console.Out.WriteLine("       -FILE jpg_file_name latitude longitude alitude");
      Console.Out.WriteLine("             latitude/longitude must be in the format XX.XXXXXX or -XX.XXXXXX");
      Console.Out.WriteLine("             altitude is good to 6 decimal places");
    }
  }
}
